package com.fire.control.system.support.util;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fire.control.system.dto.LoraDecoder;
import com.fire.control.system.entity.WarningProtocol;

public class LoraDecoderUtil {
	
	private static final Logger logger = LoggerFactory.getLogger(LoraDecoderUtil.class);
	
	public static LoraDecoder decoder(String base64Str,WarningProtocol warningProtocol){
		LoraDecoder loraDecoder = new LoraDecoder();
		try {
			byte[] bytestr = decoderToString(base64Str).getBytes();
			int tag = bytestr[warningProtocol.getEndIndex()- warningProtocol.getStartIndex()];
			if(tag == warningProtocol.getVal()){
				loraDecoder.setAlarm(true);
			}
			loraDecoder.setWarningDesc(warningProtocol.getId());
		} catch (Exception e) {
			logger.info("Lora---解码错误!",e);
		}
		return loraDecoder;
	}
	
	/*** 
     * encode by Base64 
     */ 
	private static String encodeToString(String input) throws Exception{  
    	String desc = java.util.Base64.getEncoder().encodeToString(input.getBytes(StandardCharsets.UTF_8)); 
    	return desc;
    }  
    
	
	/*** 
     * decode by Base64 
     */  
	private static String decoderToString(String input) throws Exception{  
    	String unDecodeStr = new String(Base64.getDecoder().decode(input),StandardCharsets.UTF_8);  
    	return unDecodeStr;
    }

	
	/**
	* @Title: fport_19
	* @Description: TODO(火警-0正常1报警)
	* @author wanbo
	* @date  2017年6月22日 下午4:34:40
	* @param base64Str
	* @return
	*/
	public static Integer fport_19(String base64Str){
		Integer val = null; 
		try {
			val = AsciiUtil.SumStrAscii(decoderToString(base64Str));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return val;
	}
	
	
	/**
	* @Title: fport_18
	* @Description: TODO(电池欠压-0正常1电池欠压)
	* @author wanbo
	* @date  2017年6月22日 下午4:34:25
	* @param base64Str
	* @return
	*/
	public static int fport_18(String base64Str){
		int val = 0;
		try {
			byte[] bytestr = decoderToString(base64Str).getBytes();
			val = bytestr[0];
		} catch (Exception e) {
			e.printStackTrace();
		}
		return val;
	}
	
	public static void main(String[] args) {
		//0不报警 , 1报警
		System.out.print(fport_19("AQAA"));
	}
	
}
